<?php

// Model:'ChiTietCuocKiemTra' - Database Table: 'chi_tiet_cuoc_tt'

Class ChiTietCuocKiemTra extends Eloquent {

    //Cac bang tham gia trong model
    private $tables = array("cuoc_tt", "chi_tiet_cuoc_tt", "vctcuoctt");
    //Rang buoc du lieu
    private $rules = array(
        "ID_CUOC_TT" => "required",
        "TG_LAP_BB" => "required",
        "TG_KT_BB" => "required",
        "ID_NOIDUNG_KT" => "required",
        "LY_DO_CL" => "min:1|max:500",
        "THTH_NV" => "min:1|max:200",
        "KETQUA_KT" => "min:1|max:200",
        "HANH_VI_VP" => "min:1|max:200",
        "YEU_CAU" => "min:1|max:200",
        "KIEN_NGHI" => "min:1|max:500",
        "HT_XU_PHAT" => "min:1|max:200",
        "HT_KP_HAU_QUA" => "min:1|max:200",
        "GIAY_NT_SO" => "required",
        "GHI_CHU" => "required",
        "TG_BAO_CAO" => "required",
        "TG_LAP_PHIEU_TD" => "required",
        "SO_THONG_TU" => "min:1|max:50",
        "SO_QUYET_DINH" => "min:1|max:50",
        "TG_BAN_HANH" => "required",
        "NGUOI_BAN_HANH" => "min:1|max:100",
        "SO_KL" => "min:1|max:10",
        "TG_KET_LUAN" => "required",
        "NGUOI_KY" => "min:1|max:100"
    );

    //Ham kiem tra du lieu
    public function validation($data) {
        return Validator::make($data, $this->rules);
    }

    //Lay ds du lieu
    public function GetList() {
        return DB::table($this->tables[2])
                ->select('*')
                ->first();
    }

    //Lay du lieu xac dinh
    public function GetOne($id) {
         return DB::table($this->tables[0])
                ->select('*')
                ->where($this->tables[0] . ".ID", $id)
                ->first();
    }

    public function GetOneWithMD($mst) {

        return DB::table($this->tables[2])
                ->select('*')
                ->where($this->tables[2] . ".MST", $mst)
                ->first();
    }

    //Luu du lieu moi
    public function SaveIn($data) {
        return DB::table($this->tables[1])
                        ->insert($data);
    }

    //Luu du lieu sua doi
    public function StoreIn($data, $id) {
        try {
            DB::table($this->tables[1])
                    ->where("ID", $id)
                    ->update($data);
            return true;
        } catch (Exception $ex) {
            return false;
        }
    }

    //Xoa du lieu xac dinh
    public function Remove($id) {
        try {
            return DB::table($this->tables[1])
                            ->delete($id);
        } catch (Exception $ex) {
            return false;
        }
    }

}
